<%@page import="com.lgx.mall.business.entity.Order"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="com.lgx.right.entity.User"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	<link href="<%=basePath %>style/base.css" type="text/css" rel="stylesheet" />
	<link href="<%=basePath %>style/shopping-cart.css" type="text/css" rel="stylesheet" /><!-- 购物车样式 -->
	<jsp:include page="/page/jsp/member/head.jsp" />
	<script type="text/javascript">
		//根据所选省的code找到所有parent为此code的数据
		function getCitys(){
			var selectedIndex = document.getElementById("province").selectedIndex;
			var code = document.getElementById("province")[selectedIndex].value;
			if(code == "0"){   //表示什么都没有选中
				document.getElementById("city").innerHTML = "----请选择市----";
			}else{
				var type = "city";
				var postURL = path+"/member/initCityCounty.htm";
				$.ajax({
				   type: "POST",
				   url: postURL,
				   data: "type="+type+"&code="+code,
				   success: resultHandle
				});
			}
		}
		
		function resultHandle(result)
		{
			var res = result.split("|");
			var city = document.getElementById("city");
			city.options.length = 0;
			city.options.add(new Option());
			for(var i=0;i<res.length;i++){
				var op = new Option(res[i].split(",")[1],res[i].split(",")[0]);
				city.options.add(op);
			}
		}
		
		//根据所选市code找到所有parent为此code的数据
		function getCountys(){
			var selectedIndex = document.getElementById("city").selectedIndex;
			var code = document.getElementById("city")[selectedIndex].value;
			if(code == "0"){
				document.getElementById("region").innerHTML = "----请选择县区----";
			}else{
				var type = "county";
				var postURL = path+"/member/initCityCounty.htm";
				$.ajax({
				   type: "POST",
				   url: postURL,
				   data: "type="+type+"&code="+code,
				   success: resultHandle1
				});
			}
		}
		
		function resultHandle1(result)
		{
			var res = result.split("|");
			var region = document.getElementById("region");
			region.options.length = 0;
			for(var i=0;i<res.length;i++){
				var op = new Option(res[i].split(",")[1],res[i].split(",")[0]);
				region.options.add(op);
			}
		}
		
		
		//添加或者保存修改的地址
		function save(){
			var name = escape(escape($('#name').val()));
			var tel = escape(escape($('#tel').val()));
			var province = escape(escape($('#province option:selected').text()));
			var city = escape(escape($('#city option:selected').text()));
			var region = escape(escape($('#region option:selected').text()));
			var address = escape(escape($('#address').val()));
			var zipcode = escape(escape($('#zipcode').val()));
			var isdefault = "";
			if(document.getElementById("checkbox").checked==true){   //如果选择
				isdefault = "1";
			}else{
				isdefault = "0";
			}
			
			if(moibleExits(tel) == "1"){  //表示验证通过
				var url = path+"/member/save.htm?name="+name+"&tel="+tel+"&province="+province+"&city="+city+"&region="+region+"&address="+address+"&zipcode="+zipcode+"&isdefault="+isdefault;
				$.ajax({
					   url: url,
					   success: function(data){
						   if(data=="1"){
							   window.location = path+"/member/address.htm?otype=add";
						   }else{
							   alertBox("操作失败!");
						   }
					   }
				});
			}else{
				alertBox("电话号码格式不对");
			}
		}
		
		
		
		function moibleExits(tel){  // /^0[0-9]\d{2,3}\-[2-9]\d{1}[0-9]\d{6,7}$/.test(tel)
			if(/^1[3|4|5|8][0-9]\d{8}$/.test(tel)){ 
				return "1";								
			}else{
				return "0";
			}
		}
		
		
		//管理收货地址跳转
		function address(){
			window.location = path+"/member/address.htm";
		}
		
		
		//单选按钮触发事件
		function farewayChange(orderid){
			
			if($('#addressId').val() == null){
				alertBox("请填写收货地址！");
				
				return ;
			}
			var farewayValue = "";
			$("input[name='farewayRaido']:checked").each(function(){
				farewayValue = $(this).val() ;
			});
			$('#farewayName').html($('#farewayLabel'+farewayValue+'').html());
			$('#farewayPrice').html("0");
			$('#farePrice1').html("0");
			$('#farePrice2').html("0");
			$('#fareway').attr("value",farewayValue);
			
			if(farewayValue == "1"){   //非自提方式
				//前往计算运费
				$.ajax({
					   url: path+"/cart/farewayChange.htm?addressId="+$('#addressId').val()+"&orderId="+orderid+"&farewayValue="+farewayValue,
					   success: function(data){
						   if(data != ''){   //表示计算出了运费，从而得知勾选的是需要支付运费的
							   $('#farewayPrice').html(data);
							   $('#farePrice1').html(data);
							   $('#farePrice2').html(data);
							   $('#fare').attr("value",data);
						   }
					   }
				});
			}
		}
		
		
		//提交订单
		function submitOrder(id){
			var farewayValue = "";
			$("input[name='farewayRaido']:checked").each(function(){
				farewayValue = $(this).val() ;
			});
			
			if(id==null){
				alertBox("未找到订单!");
			}else if($('#addressId').val() == null){
				alertBox("请填写收货地址！");
			}else if($('#fareway').val() == null){
				alertBox("请选择配送方式！");
			}else if($('#fare').val() == null && farewayValue == "1"){
				alertBox("请确定运费！");
			}else{
				window.location = path+"/cart/submitOrder.htm?orderId="+id+"&addressId="+$('#addressId').val()+"&fareway="+$('#fareway').val()+"&fare="+$('#fare').val();
			}
			
		}
		
		//选择收货地址
		function radioAddress(addressId){
			//alert(addressId);
			if(addressId != null){   
				$.ajax({
					   url: path+"/cart/loadAddress.htm?addressId="+addressId,
					   success: res
				});
			}
		}
		
		function res(result){
			if(result!=null){
				//开始解析
				var rs = result.split("|");
				$('#addressId').html(rs[0]);                         //重新赋id值
				$('#addressName').html(rs[1]);                       //重新赋name值
				$('#addressProvince').html(rs[2]);                   //重新赋省份值
				$('#address').html(rs[3]);                           //重新赋完整地址值
				$('#addressTel').html(rs[4]);                        //重新赋完整地址值
				$('#addressZipcode').html(rs[5]);                    //重新赋完整地址值
			}
		}
		
	</script>
  </head>
  
  <body>
  	<div class="right">
	<h1><b>提交订单</b><span><img src="<%=basePath%>images/right_h1bg2.jpg" width="3" height="29" /></span></h1>
	<div class="ri_main">
		<ol class="flow-steps">
			<li class="s1 suc">购物车</li>
			<li class="s2 cur">确认订单信息</li>
			<li class="s3 suc">成功提交订单</li>
		</ol>
   <div class="order-info">
		<div class="hd"><strong class="hd-t">确认收货地址</strong><a href="javascript:void(0);" onclick="address()" target="_blank" class="hd-act">管理收货地址</a></div>
		<div class="shipping-address">
			<input type="hidden" id="addressId" name="addressId" value="${address == null?'':address.id}"/>
			<table class="address-info">
				<tbody>
					<tr>
						<td class="th">收货人姓名：</td>
						<td><lable id="addressName">${address.name}</lable></td>
					</tr>
					<tr>
						<td class="th">省　　份：</td>
						<td><lable id="addressProvince">${address.province}</lable></td>
					</tr>
					<tr>
						<td class="th">地　　址：</td>
						<td><lable id="address">${address.getAddressAll()}</lable></td>
					</tr>
					<tr>
						<td class="th">手机号码：</td>
						<td><lable id="addressTel">${address.tel}</lable></td>
					</tr>
					<tr>
						<td class="th">邮　　编：</td>
						<td><lable id="addressZipcode">${address.zipcode}</lable></td>
					</tr>
				</tbody>
				<tfoot>
					<tr>
						<td colspan="2">
							<a href="#" class="add-addr-act" id="J-add-addr">使用新地址</a><span class="edit-tip" id="J-edit-addr-tip">收货地址已更新！</span>
						</td>
					</tr>
				</tfoot>
			</table>
			
			<!-- 如果点击了《使用新地址》的时候，出现之前填写过的地址，或者让用户新填写一个地址 -->
			<div class="edit-address">
				<table class="address-list">
					<tbody>
						<c:forEach var="addr" items="${ads}" varStatus="s">
							<tr>
								<td colspan="2"><input type="radio" name="address" onchange="radioAddress(${addr.id})" id="address1" /><label for="address1">${addr.province} ${addr.city} ${addr.region} ${addr.address }</label></td>
							</tr>
						</c:forEach>
					</tbody>
				</table>
			
				<div class="hd"><strong class="hd-t">添加新地址</strong></div>
				<table class="new-address">
					<tbody>
						<tr>
							<td class="th"><em class="mark">*</em>收货人姓名：</td>
							<td><input type="text" id="name" name="name" /></td>
						</tr>
						<tr>
							<td class="th"><em class="mark">*</em>收货人电话：</td>
							<td><input type="text" name="tel" id="tel" /></td>
						</tr>
						<tr>
							<td class="th"><em class="mark">*</em>送货地址：</td>
							<td>
								<select name="province" id="province" onchange="getCitys()">
						          <option>----请选择省份----</option>
					          		<c:forEach items="${provinces}" var="province" varStatus="pstatus">
										<c:choose>
											<c:when test="${!empty address && address.province==province.name}"><option value="${province.code }" selected="selected">${province.name }</option></c:when>
											<c:otherwise><option value="${province.code }">${province.name }</option></c:otherwise>
										</c:choose>
							  		</c:forEach>
						        </select>
						        
						        <select name="city" id="city" onchange="getCountys()">
						         	<c:if test="${!empty address}">
										<option value="${address.city }" selected="selected">${address.city }</option>
									</c:if>
						        </select>
						        
						        <select name="region" id="region">
						          	<c:if test="${!empty address}">
										<option value="${address.region }" selected="selected">${address.region }</option>
									</c:if>
						        </select>
							</td>
						</tr>
						<tr>
							<td class="th"><em class="mark">*</em>街道地址：</td>
							<td bgcolor="#FFFFFF"><input type="text" name="address" id="address" /></td>
						</tr>
						<tr>
							<td class="th">邮政编码：</td>
							<td><input type="text" id="zipcode" name="zipcode" /> <span class="mes">有助于快速确定送货地址</span></td>
						</tr>
						<tr>
					        <td align="right" bgcolor="#FFFFFF">是否为默认地址：</td>
					        <td bgcolor="#FFFFFF"><input name="checkbox" type="checkbox" id="checkbox" /></td>
					   </tr>
					</tbody>
				</table>
				
				<div class="edit-act">
					<input type="submit" onclick="save()" value="保存收货人信息" class="sub" /><input type="reset" value="取消" class="res" />
				</div>
			</div>			
		</div>
		
		<div class="hd"><strong class="hd-t">配送方式</strong><a href="#" class="modify" id="J-edit-delivery">[修改]</a><span class="edit-tip" id="J-edit-deli-tip">送货方式已更新！</span></div>
		<div class="delivery">
			<input type="hidden" id="fareway" name="fareway" value="${order.fareway}"/>
			<input type="hidden" id="fare" name="fare" value="${order.fare}"/>
			<table class="delivery-info">
				<tbody>
					<tr>
						<td class="th">配送方式：</td>
						<td><label id="farewayName">
							<c:forEach var="fareway" items="${fareways}">
								<c:if test="${fareway.code == order.fareway}">
									<c:out value="${fareway.name }"></c:out>
								</c:if>
							</c:forEach>
						</label></td>
					</tr>
					<tr>
						<td class="th">运　　费：</td>
						<td><label id="farewayPrice"><c:out value="${order.fare }"></c:out></label>元</td>
					</tr>
				</tbody>
			</table>
			
			<div class="edit-delivery">
				<div class="hd"><strong class="hd-t">使用新的配送方式</strong></div>
				<table class="new-delivery">
					<tbody>
						<tr>
							<td class="th"><em class="mark">*</em>送货方式：</td>
							<td>
								<c:forTokens items="${order.fareway }" delims="," var="fareway1">
									<c:forEach var="fareway2" items="${fareways}">
										<c:if test="${fareway2.code == fareway1}">
											<input type="radio" name="farewayRaido" id="farewayRaido${fareway1}" checked="${order.fareway == fareway1?'checked':''}" onchange="farewayChange(${order.id})" value="${fareway1}"/><label id="farewayLabel${fareway1}" for="farewayRaido${fareway1}">${fareway2.name}</label>
										</c:if>
									</c:forEach>
						        </c:forTokens>
								
							</td>
						</tr>
						<tr>
							<td class="th"> </td>
							<td class="mes">声明：我们会努力按照您指定的时间配送，但因天气、交通等各类因素影响，您的订单有可能会有延误现象！敬请谅解！</td>
						</tr>
						<!-- <tr>
							<td class="th">送货前电话确认：</td>
							<td>
								<input type="radio" name="radio1" value="1" /><label for="">是</label>
								<input type="radio" name="radio1" value="0" /><label for="">否</label>
							</td>
						</tr> -->
					</tbody>
				</table>
				<!-- <div class="edit-act">
					<input type="submit" value="保存配送信息" class="sub" /><input type="reset" value="取消" class="res" />
				</div> -->
			</div>
		</div>
		
		<!-- S 商品清单 -->
		<div class="hd"><strong class="hd-t">商品清单</strong><a href="#" target="_blank" class="hd-act">返回修改购物车</a></div>
		<table class="shop-pro-list">
			<thead>
				<tr>
					<th>商品编号</th>
					<th>商品名称</th>
					<th>积分</th>
					<th>库存状态</th>
					<th>商品数量</th>
				</tr>
			</thead>
			<tbody>
				<c:forEach var="ods" items="${orderDetails}">
					<tr>
						<td><a href="${ods.entityUrl}" target="_blank">${ods.entityId}</a></td>
						<td><a href="${ods.entityUrl}" target="_blank">${ods.entityName}</a></td>
						<td>${ods.price}</td>
						<td>现货</td>
						<td>${ods.amount}</td>
					</tr>
				</c:forEach>
			</tbody>
			
		</table>
		<!-- E 商品清单 -->
		
		<!-- S 结算信息 -->
		<div class="hd"><strong class="hd-t">结算信息</strong></div>
		<table class="settlement" id="jiesuan">
			<tbody>
				<tr>
					<td class="info">商品金额：${order.score} 积分   + 运费：<lable id="farePrice1"><c:out value="${order.fare }"></c:out></lable> 元</td>
				</tr>
				<tr>
					<td>应付积分：<em>${order.score} 积分</em></td>
				</tr>
					<tr>
						<td>应付运费：<em><lable id="farePrice2"><c:out value="${order.fare }"></c:out></lable> 元</em></td>
					</tr>
				<tr>
					<td><input type="button" onclick="submitOrder(${order.id})" value="提交订单" class="act" /></td>
				</tr>
			</tbody>
		</table>
		<!-- S 商品清单 -->
	</div>
	<!-- E 编辑订单信息时显示 -->
	</div>
	</div>
	
<!-- S 自定义脚本 -->
<script src="<%=basePath%>js/jquery-1.3.2.min.js"></script>
<script src="<%=basePath%>js/jquery.soChange-min.js"></script>
<script src="<%=basePath%>js/shop-car.js"></script>
<script src="<%=basePath%>js/tab.js"></script>

<!-- E 自定义脚本 -->
  </body>
</html>
